<?php

namespace app\admin\model\fastim;

use think\Model;
use think\Db;
use traits\model\SoftDelete;


class User extends Model
{
    use SoftDelete;
    
    // 表名
    protected $name = 'fastim_user';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_text',
        'gender_text',
        'status_text'
    ];

    protected static function init()
    {
        self::beforeUpdate(function ($row) {
            $changed = $row->getChangedData();
            //如果有修改密码
            if (isset($changed['password'])) {
                if ($changed['password']) {
                    $salt = \fast\Random::alnum();
                    $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
                    $row->salt = $salt;
                    \app\admin\model\User::where('id', $row->user_id)->update([
                        'password' => $row->password,
                        'salt' => $row->salt,
                    ]);
                } else {
                    unset($row->password);
                }
            }
            //修改手机号
            if (isset($changed['mobile'])) {
                if ($changed['mobile']) {
                    \app\admin\model\User::where('id', $row->user_id)->update([
                        'mobile' => $row->mobile
                    ]);
                }
            }
            //登录限制ip
            if (isset($changed['ip'])) {
                if ($changed['ip']) {
                    \app\admin\model\User::where('id', $row->user_id)->update([
                        'ip' => $row->ip
                    ]);
                }
            }
        });
    }

    public function getTypeList()
    {
        $openCsr = Db::name('fastim_config')->where('name', 'open_csr')->value('value');
        $data = ['user' => __('Type user')];
        if($openCsr) {
            $data['csr'] = __('Type csr');
            $data['tourist'] = __('Type tourist');
        }
        return $data;
    }

    public function getGenderList()
    {
        return ['secrecy' => __('Gender secrecy'), 'male' => __('Gender male'), 'female' => __('Gender female')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list  = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getGenderTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
        $list  = $this->getGenderList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function fauser()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function csrgroup()
    {
        return $this->belongsTo('app\admin\model\fastim\CsrGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function usergroup()
    {
        return $this->belongsTo('app\admin\model\fastim\UserGroup', 'user_group_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
